//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BlackVolSurface.h"
using namespace Cephei::QL::Experimental::Volatility;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Experimental/Volatility/BlackAtmVolCurve.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures::Volatility;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Volatility::CBlackVolSurface::CBlackVolSurface (boost::shared_ptr<QuantLib::BlackVolSurface>& childNative, Object^ owner) : CBlackAtmVolCurve(CBlackVolSurface::typeid)
{
#ifdef HANDLE
	_phBlackVolSurface = NULL;
#endif
	_ppBlackVolSurface = &childNative;
    _ppBlackAtmVolCurve = new boost::shared_ptr<QuantLib::BlackAtmVolCurve> (boost::dynamic_pointer_cast<QuantLib::BlackAtmVolCurve> (*_ppBlackVolSurface));
}
Cephei::QL::Experimental::Volatility::CBlackVolSurface::CBlackVolSurface (QuantLib::BlackVolSurface& childNative, Object^ owner) : CBlackAtmVolCurve(CBlackVolSurface::typeid)
{
#ifdef HANDLE
	_phBlackVolSurface = NULL;
#endif
	_ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (&childNative);
    _ppBlackAtmVolCurve = new boost::shared_ptr<QuantLib::BlackAtmVolCurve> (boost::dynamic_pointer_cast<QuantLib::BlackAtmVolCurve> (*_ppBlackVolSurface));
    _BlackVolSurfaceOwner = owner;
    _BlackAtmVolCurveOwner = owner;
}

Cephei::QL::Experimental::Volatility::CBlackVolSurface::CBlackVolSurface (CBlackVolSurface^ copy) : CBlackAtmVolCurve(CBlackVolSurface::typeid)
{
#ifdef HANDLE
	_phBlackVolSurface = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (copy->GetShared());
        _ppBlackAtmVolCurve = new boost::shared_ptr<QuantLib::BlackAtmVolCurve> (boost::dynamic_pointer_cast<QuantLib::BlackAtmVolCurve> (*_ppBlackVolSurface));
    }
}
Cephei::QL::Experimental::Volatility::CBlackVolSurface::CBlackVolSurface (System::Type^ t) : CBlackAtmVolCurve(CBlackVolSurface::typeid)
{
#ifdef HANDLE
	_phBlackVolSurface = NULL;
#endif
	if (!t->IsSubclassOf(CBlackVolSurface::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Volatility::CBlackVolSurface::CBlackVolSurface (QuantLib::Handle<QuantLib::BlackVolSurface>& childNative, Object^ owner)  : CBlackAtmVolCurve(CBlackVolSurface::typeid)
{
	_phBlackVolSurface = &childNative;
	_ppBlackVolSurface = &static_cast<boost::shared_ptr<QuantLib::BlackVolSurface>>(childNative.currentLink());
    _ppBlackAtmVolCurve = new boost::shared_ptr<QuantLib::BlackAtmVolCurve> (boost::dynamic_pointer_cast<QuantLib::BlackAtmVolCurve> (*_ppBlackVolSurface));
    _BlackVolSurfaceOwner = owner;
}
Cephei::QL::Experimental::Volatility::CBlackVolSurface::CBlackVolSurface (QuantLib::Handle<QuantLib::BlackVolSurface> childNative)  : CBlackAtmVolCurve(CBlackVolSurface::typeid)
{
	_phBlackVolSurface = &childNative;
	_ppBlackVolSurface = &static_cast<boost::shared_ptr<QuantLib::BlackVolSurface>>(childNative.currentLink());
    _ppBlackAtmVolCurve = new boost::shared_ptr<QuantLib::BlackAtmVolCurve> (boost::dynamic_pointer_cast<QuantLib::BlackAtmVolCurve> (*_ppBlackVolSurface));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Volatility::CBlackVolSurface::CBlackVolSurface (QuantLib::BlackVolSurface childNative)  : CBlackAtmVolCurve(CBlackVolSurface::typeid)
{
#ifdef HANDLE
	_phBlackVolSurface = NULL;
#endif
	_ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (new QuantLib::BlackVolSurface (childNative));
    _ppBlackAtmVolCurve = new boost::shared_ptr<QuantLib::BlackAtmVolCurve> (boost::dynamic_pointer_cast<QuantLib::BlackAtmVolCurve> (*_ppBlackVolSurface));
}
#endif

Cephei::QL::Experimental::Volatility::CBlackVolSurface::~CBlackVolSurface ()
{
    if (_ppBlackVolSurface != NULL)
    {
	    delete _ppBlackVolSurface;
        _ppBlackVolSurface = NULL;
    }
}
Cephei::QL::Experimental::Volatility::CBlackVolSurface::!CBlackVolSurface ()
{
    if (_ppBlackVolSurface != NULL)
    {
	    delete _ppBlackVolSurface;
    }
}
QuantLib::BlackVolSurface& Cephei::QL::Experimental::Volatility::CBlackVolSurface::GetReference ()
{
    if (_ppBlackVolSurface == NULL) throw gcnew NativeNullException ();
	return **_ppBlackVolSurface;
}
boost::shared_ptr<QuantLib::BlackVolSurface>& Cephei::QL::Experimental::Volatility::CBlackVolSurface::GetShared ()
{
    if (_ppBlackVolSurface == NULL) throw gcnew NativeNullException ();
	return *_ppBlackVolSurface;
}
QuantLib::BlackVolSurface* Cephei::QL::Experimental::Volatility::CBlackVolSurface::GetPointer ()
{
    if (_ppBlackVolSurface == NULL) throw gcnew NativeNullException ();
	return &**_ppBlackVolSurface;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackVolSurface>& Cephei::QL::Experimental::Volatility::CBlackVolSurface::GetHandle ()
{
	if (_phBlackVolSurface == NULL)
	{
		_phBlackVolSurface = new Handle<QuantLib::BlackVolSurface> (*_ppBlackVolSurface);
	}
	return *_phBlackVolSurface;
}
#endif
bool Cephei::QL::Experimental::Volatility::CBlackVolSurface::HasNative () 
{
	return (_ppBlackVolSurface != NULL);
}

Cephei::QL::Termstructures::Volatility::ISmileSection^ Cephei::QL::Experimental::Volatility::CBlackVolSurface::SmileSection (Double prm1, Boolean extrapolate)
{
    try
    {
        QuantLib::Time _prm1 = (QuantLib::Time)ValueHelper::Convert (prm1);
        bool _extrapolate = (bool)ValueHelper::Convert (extrapolate);
    	boost::shared_ptr<QuantLib::SmileSection> _rv = (boost::shared_ptr<QuantLib::SmileSection>)(*_ppBlackVolSurface)->smileSection ( _prm1,  _extrapolate );   
        Cephei::QL::Termstructures::Volatility::CSmileSection^ _nrv = gcnew Cephei::QL::Termstructures::Volatility::CSmileSection (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::ISmileSection^ Cephei::QL::Experimental::Volatility::CBlackVolSurface::SmileSection (DateTime prm1, Boolean extrapolate)
{
    try
    {
        QuantLib::Date _prm1 = (QuantLib::Date)ValueHelper::Convert (prm1);
        bool _extrapolate = (bool)ValueHelper::Convert (extrapolate);
    	boost::shared_ptr<QuantLib::SmileSection> _rv = (boost::shared_ptr<QuantLib::SmileSection>)(*_ppBlackVolSurface)->smileSection ( _prm1,  _extrapolate );   
        Cephei::QL::Termstructures::Volatility::CSmileSection^ _nrv = gcnew Cephei::QL::Termstructures::Volatility::CSmileSection (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::ISmileSection^ Cephei::QL::Experimental::Volatility::CBlackVolSurface::SmileSection (Cephei::QL::Times::IPeriod^ prm1, Boolean extrapolate)
{
    CPeriod^ _Cprm1;
    try
    {
        _Cprm1 = safe_cast<CPeriod^> (prm1);
        _Cprm1->Lock();
        QuantLib::Period& _prm1 = static_cast<QuantLib::Period&> (_Cprm1->GetReference ()); 
        bool _extrapolate = (bool)ValueHelper::Convert (extrapolate);
    	boost::shared_ptr<QuantLib::SmileSection> _rv = (boost::shared_ptr<QuantLib::SmileSection>)(*_ppBlackVolSurface)->smileSection ( _prm1,  _extrapolate );   
        Cephei::QL::Termstructures::Volatility::CSmileSection^ _nrv = gcnew Cephei::QL::Termstructures::Volatility::CSmileSection (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

